ActiveAdmin.register LimitList do
  menu priority: 6, label: '涨停板数据'
  scope :last_pretrade_date, :default => true
  scope :last_cal_date
  scope :all
  # 查询过滤
  filter :trade_date
  filter :ts_code
  filter :reason
  filter :industry, as: :select

  # 列表显示
  index do
    selectable_column
    column :industry
    column :close
    column :fd_amount
    column :pct_chg
    column '实时涨幅' do |l|
      "#{(JSON.parse $Redis.get(l.ts_code[0..5]))["changepercent"]&.to_f.round(2)}%" unless $Redis.get(l.ts_code[0..5]).nil?
    end
    column '实时换手' do |l|
      "#{(JSON.parse $Redis.get(l.ts_code[0..5]))["turnoverratio"]&.to_f.round(2)}%" unless $Redis.get(l.ts_code[0..5]).nil?
    end
    column :limit_times
    column :year_count
    column :trade_date
    column :ts_code
    column :name
    column :ths_flow
    column :score
    column :reason
    column :created_at if Rails.env.development?
    actions
  end

  # 表单配置
  form do |f|
    f.inputs do
      f.input :trade_date, as: :datepicker
      f.input :ts_code
      f.input :industry
      f.input :name
      f.input :close
      f.input :pct_chg
    end
    f.actions
  end

  # CSV 导出配置
  csv do
    column :trade_date
    column :ts_code
    column :name
    column :industry
    column :close
    column :pct_chg
    column :amount
    column :limit_amount
    column :float_mv
    column :total_mv
    column :turnover_ratio
    column :fd_amount
    column :limit_times
  end

  # 性能优化
  config.sort_order = 'trade_date_desc'
  config.per_page = 100
  config.batch_actions = false
end

# redis
# {"symbol"=>"sh600011",
#  "code"=>"600011",
#  "name"=>"华能国际",
#  "trade"=>"7.140",
#  "pricechange"=>-0.05,
#  "changepercent"=>-0.695,
#  "buy"=>"7.130",
#  "sell"=>"7.140",
#  "settlement"=>"7.190",
#  "open"=>"7.180",
#  "high"=>"7.180",
#  "low"=>"7.090",
#  "volume"=>18391900,
#  "amount"=>131044670,
#  "ticktime"=>"10:59:53",
#  "per"=>15.522,
#  "pb"=>1.958,
#  "mktcap"=>11208438.658326,
#  "nmc"=>7852364.882166,
#  "turnoverratio"=>0.16723}

#  id: 746,
#  trade_date: "2025-04-18",
#  ts_code: "836892.BJ",
#  industry: "工程咨询",
#  name: "广咨国际",
#  close: 0.2176e2,
#  pct_chg: 0.2999e2,
#  amount: 0.261942886e9,
#  limit_amount: nil,
#  float_mv: 0.290603370368e10,
#  total_mv: 0.336985950592e10,
#  turnover_ratio: 0.1012e2,
#  fd_amount: 0.29132636e8,
#  first_time: "142645",
#  last_time: "144600",
#  open_times: 1,
#  up_stat: "1/1",
#  limit_times: 1,
#  limit: "U",
#  year_count: nil,
#  reason: nil,